package com.sino.util.page;

import javax.servlet.http.HttpServletRequest;

public class PagerQueryCondition {
	private String condition = "";// 查询参数

	private String hqlWhere = "";// 查询条件
	
	private HttpServletRequest req;

	// 根据请求对象初始化condition和hqlWhere
	// condition查询参数，构造查询参数为了分页传递到下一页
	// hqlWhere查询条件，构造查询条件，用于查询满足条件的数据
	// 用法：调用此方法传递一个请求对象，初始化上诉两个参数之后，即可以用getCondition（），getHqlWhere两个方法获得参数值
	public void initializtion(HttpServletRequest request) {
		req=request;
		StringBuffer bf = new StringBuffer();
		String paraName;
		String hqlParaName;
		java.util.Enumeration em = request.getParameterNames();
		while (em.hasMoreElements()) {
			paraName = em.nextElement().toString();
			hqlParaName = paraName.replace('$', '.');
			// 获得查询参数

			if (request.getParameter(paraName) != null
					&& !(request.getParameter(paraName).equals(""))
					&& !(paraName.equals("pager.offset"))) {
				if ("".equals(condition)) {
					condition ="&"+ paraName + "=" + request.getParameter(paraName);
				} else {
					condition = condition + "&" + paraName + "="
							+ request.getParameter(paraName);
				}
				if (bf.length() == 0) {
					bf.append(hqlParaName + " like '%"
							+ request.getParameter(paraName) + "%'");
				} else {
					bf.append(" and " + hqlParaName + " like '%"
							+ request.getParameter(paraName) + "%'");
				}

			}

		}
		hqlWhere = bf.toString();

	}

	public void setRequestAttribute(String param,String paramValue)
	{
		req.setAttribute(param,paramValue);
	}
	
	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public String getHqlWhere() {
		return hqlWhere;
	}

	public void setHqlWhere(String hqlWhere) {
		this.hqlWhere = hqlWhere;
	}
}
